var LoggedInUser = {
    userUri: null,
    userDetails: null,
    loadUser: function() {
        console.log("LoggedInUser.loadUser");
        Ext.Ajax.request({
            url: "/addama-users/whoami",
            method: "GET",
            success: function(response) {
                var usersDecoded = Ext.util.JSON.decode(response.responseText);
                LoggedInUser.userUri = usersDecoded.user;
                //var utokens = LoggedInUser.userUri.split("/");
                //Ext.getCmp("limsSignoutButtonId").setTooltip("Logout " + utokens[utokens.length-1].split("@")[0].toUpperCase());
            },
            failure: function() {
                eventManager.fireStatusMessageEvent({ text: "You are not logged in", level: "error"});
            }
        });
    },
    
    loadUserDetails: function() {
        console.log("LoggedInUser.loadUserDetails");
        Ext.Ajax.request({
            url: LoggedInUser.userUri,
            method: "GET",
            success: function(response) {
                LoggedInUser.userDetails = Ext.util.JSON.decode(response.responseText);

                var label = new Ext.form.Label({
                    renderTo: "header",
                    text: "Welcome " + LoggedInUser.userDetails.fullName,
                    style: "color:white;float:left;margin-left:750px;margin-top:-19px"
                });
                label.render();
            },
            failure: function() {
                eventManager.fireStatusMessageEvent({ text: "You are not logged in", level: "error"});
            }
        });
    },

    signOut: function() {
        console.log("LoggedInUser.signOut");
        LoggedInUser.userUri = null;
        LoggedInUser.userDetails = null;
	//LoggedInUser.homePreference = null;
        Ext.Ajax.request({
           url: "/addama-login/logout",
           success: function() {
               document.location = "/addama-html/login.html?login_success=" + document.location.href;
           },
           failure: function() {
               document.location = "/addama-html/login.html?login_success=" + document.location.href;
           }
        });        
    }
};

Ext.onReady(function() {
    LoggedInUser.loadUser();
});
